package com.tom.architect.day11;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

// GridView样式的分割线
public class GridLayoutItemDecoration extends RecyclerView.ItemDecoration {
    private Drawable mDivider;

    public GridLayoutItemDecoration(Context context, int drawableResId) {
        // 获取 Drawable
        mDivider = ContextCompat.getDrawable(context, drawableResId);
    }

    // 留出分割线的位置：下边和右边。
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        /* // 最右边和最底部，也存在分割线
        outRect.right = mDivider.getIntrinsicWidth();
        outRect.bottom = mDivider.getIntrinsicHeight();*/

        //留出位置 下面和右边
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        int spanCount = getColumnCount(layoutManager);
        if(layoutManager instanceof GridLayoutManager) {
            int childLayoutPosition = parent.getChildLayoutPosition(view);
            //判断是不是最后一列
            if((childLayoutPosition + 1 ) % spanCount != 0){
                outRect.right = mDivider.getIntrinsicWidth();
            }
        }
        //最后一行 当前位置 >（行数 - 1） * 列数
        int itemCount = parent.getAdapter().getItemCount();
        //计算有多少行 不能整除的时候需要加 1
        int rowCount = itemCount % spanCount == 0 ? itemCount / spanCount :  itemCount / spanCount  + 1;
        //获取当前位置
        int mCurrentPosition = parent.getChildLayoutPosition(view);
        if ( mCurrentPosition < (rowCount - 1)*spanCount ){
            outRect.bottom = mDivider.getIntrinsicHeight();
        }
    }

    /**
     * 获取列数
     * @param layoutManager
     * @return
     */
    private int getColumnCount(RecyclerView.LayoutManager layoutManager){
        if(layoutManager instanceof GridLayoutManager) {
            GridLayoutManager lm = (GridLayoutManager) layoutManager;
            return lm.getSpanCount(); // 获取列数
        }
        return 1;
    }

    // 绘制分割线
    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        drawHorizontal(canvas, parent);
        drawVertical(canvas, parent);
    }

    // 绘制垂直方向上的分割线。
    private void drawVertical(Canvas canvas, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) childView.getLayoutParams();
            int top = childView.getTop() - params.topMargin;
            int bottom = childView.getBottom() + params.bottomMargin;
            int left = childView.getRight() + params.rightMargin;
            int right = left + mDivider.getIntrinsicWidth();
            mDivider.setBounds(left, top, right, bottom); // 计算水平分割线的位置
            mDivider.draw(canvas);
        }
    }

    // 绘制水平分割线的位置。
    private void drawHorizontal(Canvas canvas, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = parent.getChildAt(i);
            // 处理item有margin的问题。
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) childView.getLayoutParams();
            int left = childView.getLeft() - params.leftMargin;
            int right = childView.getRight() + mDivider.getIntrinsicWidth() + params.rightMargin;
            int top = childView.getBottom() + params.bottomMargin;
            int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom); // 计算水平分割线的位置
            mDivider.draw(canvas);
        }
    }
}
